package com.oocl.crm.controller.impl;

import java.util.Scanner;

import com.oocl.crm.controller.Controller;
import com.oocl.crm.dao.impl.CustomerDaoImpl;
import com.oocl.crm.pojo.Customer;
import com.oocl.crm.util.Show;

public class ControllerImpl implements Controller{

	public void commandProcess() {
		CustomerDaoImpl customerDao = new CustomerDaoImpl();
		Show show=new Show();
		Show.showCommand();
	
		Scanner sc=new Scanner(System.in);
		//获取用户输入的字符串
		String str;
		str=sc.nextLine();
		//只要没有输入Q,程序就不结束
		while(!str.equals("Q"))
		{
			//如果忘记了命令，可以随时输入H查看命令
			if(str.equals("H"))
			{
				Show.showCommand();
				str=sc.nextLine();
				continue;
			}
			//排序
			if(str.equals("O id"))
			{
				customerDao.BubbleSortId();
				customerDao.displayAll();
				
				System.out.print("\n请输入命令：");
				str=sc.nextLine();
				continue;
			}
			if(str.equals("O name"))
			{
				customerDao.BubbleSortName();
				customerDao.displayAll();
				
				System.out.print("\n请输入命令：");
				str=sc.nextLine();
				continue;
			}
			//Delete
			if(str.charAt(0)=='D')
			{
				String id=str.substring(2);
				try {
					customerDao.delete(id);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				customerDao.displayAll();
				
				System.out.print("\n请输入命令：");
				str=sc.nextLine();
				continue;
			}
			//Add
			if(str.charAt(0)=='A')
			{
				String attri_str=str.substring(2);
				String[] attri=attri_str.split(",");
				String name=attri[0].substring(5);
				String sex=attri[1].substring(4);
				customerDao.add(name, sex);
				customerDao.displayAll();
				
				System.out.print("\n请输入命令：");
				str=sc.nextLine();
				continue;
			}
			//Search
			if(str.charAt(0)=='L')
			{
				if(str.equals("L"))
				{
					customerDao.displayAll();
				}
				else
				{
					String attri_str=str.substring(2);
					String[] attri=attri_str.split(",");
					if(attri.length==1)
					{
						String id=attri[0].substring(3);
						Customer customer=customerDao.search(id);
						System.out.println(customer);
					}
					else
					{
						String name=attri[0].substring(5);
						String sex=attri[1].substring(4);
						Customer customer=customerDao.search1(name,sex);
						System.out.println(customer);
						
					}
				}
				System.out.print("\n请输入命令：");
				str=sc.nextLine();
				continue;
			}
			//Change
			if(str.charAt(0)=='U')
			{
				String id=str.substring(2,5);
				System.out.println(id);
				String attri_str=str.substring(6);
				String[] attri=attri_str.split(",");
				if(attri.length==1)
				{
					String name=attri[0].substring(5);
					customerDao.change(id, name);
				}
				else
				{
					String name=attri[0].substring(5);
					String phone=attri[1].substring(6);
					customerDao.change(id, name, phone);
				}
				
				customerDao.displayAll();
			}

			System.out.print("\n请输入命令：");
			str=sc.nextLine();
		}
	}

}
	
